﻿Imports FastReport.Report
Imports DevExpress.XtraGrid.Views.Grid
Imports MySql.Data.MySqlClient

Public Class frmNewRepairForm
    Public Primary_Key As Integer = -1
    Public Primary_Detail_Key = -1
    Public Curr_RowID As Integer = -1
    Public DocNO As Integer = 0
    Dim Strsql As String
    Dim _mysql As New ClsSQLhelper
    Public Report_Name As String
    Public on_load As Boolean = True



    Dim ClsMysql As New ClsSQLhelper
    Dim MyConnq As New MySql.Data.MySqlClient.MySqlConnection
    Dim DA As MySql.Data.MySqlClient.MySqlDataAdapter

    Dim MyCmdBuilder As MySql.Data.MySqlClient.MySqlCommandBuilder
    Dim MySQLCMD As MySql.Data.MySqlClient.MySqlCommand
    Dim DS As New DataSet
    Dim DT As New DataTable
    Dim DR As DataRow

#Region "Function"

    Sub Load_Department()
        Try
            Dim DT_Department As New DataTable
            Strsql = "Select dep_id,dep_name from departments order by dep_name "
            DT_Department = _mysql.GetMYSQLDataTable(Strsql, "departments")
            With CboDepartments

                .DataSource = DT_Department
                .DisplayMember = "dep_name"
                .ValueMember = "dep_id"
                .SelectedValue = DT_Department.Rows(0).Item("dep_id")
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Department"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub

    Sub Load_repair_place()
        Try
            Dim DT_Department As New DataTable
            Strsql = "Select repair_place_id,repair_place_name from repair_place  "
            DT_Department = _mysql.GetMYSQLDataTable(Strsql, "repair_place")
            With CboRepairPlace

                .DataSource = DT_Department
                .DisplayMember = "repair_place_name"
                .ValueMember = "repair_place_id"
                .SelectedValue = DT_Department.Rows(0).Item("repair_place_id")
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_repair_place"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
    Sub Load_Data()
        Try
            Strsql = "select doc_detail_id,doc_head_id,doc_rowid,productname,serialno,band,model,priceperunit,vat,priceincludevat,drawqty,receiveqty,totalpriceprice,staff,remarks,purchase_date,mdf_date,warantry from pr_detail where doc_head_id = " & Primary_Key
            Dim DT_pr_detail As New DataTable
            DT_pr_detail = _mysql.GetMYSQLDataTable(Strsql, "pr_detail")

            GridControl1.DataSource = DT_pr_detail


        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Data"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
    Sub Load_Order_Status()
        Try
            Dim DT_Department As New DataTable
            Strsql = "Select repair_status_id,repair_status_name from repair_status "
            DT_Department = _mysql.GetMYSQLDataTable(Strsql, "repair_status")
            With CboStatus

                .DataSource = DT_Department
                .DisplayMember = "repair_status_name"
                .ValueMember = "repair_status_id"
                .Text = DT_Department.Rows(0).Item("repair_status_name")
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Order_Status"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
    Sub Load_BudgetYear()

        Try

            Dim MinYear As Integer
            MinYear = 2542

            If MinYear = 0 Then
                Me.CboBudgetYear.Properties.Items.Add(Now.Year)
            Else
                For i As Integer = (Now.Year + 543) To MinYear Step -1
                    Me.CboBudgetYear.Properties.Items.Add(i)
                    i = +i
                Next
            End If
            Me.CboBudgetYear.Text = (Now.Year + 543)
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_BudgetYear"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
    Sub Load_Employee()
        Try
            Dim DT_Department As New DataTable
            Strsql = "Select emp_id,emp_fname,emp_lname,concat(emp_fname,emp_lname) as fullname,active from employee  order by emp_fname,emp_lname "
            DT_Department = _mysql.GetMYSQLDataTable(Strsql, "employee")
            With CboEmployee

                .DataSource = DT_Department
                .DisplayMember = "fullname"
                .ValueMember = "emp_id"
                .Text = DT_Department.Rows(0).Item("fullname")
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Employee"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
    Sub Load_Computer()
        Try

            Dim DT_Department As New DataTable
            Strsql = "Select computername,id from computerinfo where department = " & Me.CboDepartments.SelectedValue & "  order by computername"
            DT_Department = _mysql.GetMYSQLDataTable(Strsql, "computerinfo")
            With CboComputer

                .DataSource = DT_Department
                .DisplayMember = "computername"
                .ValueMember = "id"
                If DT_Department.Rows.Count = 0 Then
                    Me.CboComputer.Enabled = False

                Else
                    Me.CboComputer.Enabled = True
                    .Text = DT_Department.Rows(0).Item("computername")
                End If

            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Computer"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
    Function Get_NewOrderNO() As String
        Try
            Dim LasID As Integer = 0
            Dim RecCont As Integer = 0
            Dim MyOrderName As String = ""
            Strsql = "Select count(repair_order_id)  as cc from repair_order where month(repair_order_date) = " & Now.Month & " and year(repair_order_date) = " & Now.Year & " order by repair_order_id desc limit 1"
            Dim DT_Order As DataTable
            RecCont = _mysql.MySQLExecuteScalar(Strsql)
            If RecCont = 0 Then
                MyOrderName = "IT-R" & (Now.Year + 543).ToString & "-" & Now.Month.ToString("0#") & "/"

                MyOrderName = MyOrderName & "01"


            Else

                Strsql = "Select ifnull(repair_order_id,0) as repair_order_id from repair_order where month(repair_order_date) = " & Now.Month & " and year(repair_order_date) = " & Now.Year & " order by repair_order_id desc limit 1"


                DT_Order = _mysql.GetMYSQLDataTable(Strsql, "repair_order")
                LasID = Val(DT_Order.Rows(0).Item("repair_order_id"))
                MyOrderName = "IT-R" & (Now.Year + 543).ToString & "-" & Now.Month.ToString("0#") & "/"
                If Len((LasID + 1).ToString) = 1 Then
                    MyOrderName = MyOrderName & "0" & (LasID + 1).ToString

                End If
            End If


            Return MyOrderName
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Get_NewOrderNO"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try

    End Function
    Sub Load_RepairDetail()
        Try
            Strsql = "Select * from repair_detail where repair_order_id = " & Primary_Key
            DA = New MySql.Data.MySqlClient.MySqlDataAdapter(Strsql, MyConnq)
            MyConnq.ConnectionString = ClsSQLhelper.MySQLConnectionStr
            MyCmdBuilder = New MySqlCommandBuilder(DA)

            'DS = New DataSet("DS_repair_detail")
            DS = New WHISDS

            DA.Fill(DS)

            With GridControl1
                .DataSource = DS.Tables(0)
                TxtOrderNO.NullText = Primary_Key

            End With
        Catch ex As Exception

        End Try
    End Sub
    Sub Load_Item()
        Try
            Strsql = "Select itemnumber,itemname from itemtype order by itemname"
            Dim DT As DataTable
            DT = _mysql.GetMYSQLDataTable(Strsql, "itemtype")
            With CboItem
                .DataSource = DT
                .DisplayMember = "itemname"
                .ValueMember = "itemnumber"
            End With
        Catch ex As Exception

        End Try
    End Sub
#End Region



    Private Sub frmNewRepairForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Load_Employee()
        Load_Item()
        Load_BudgetYear()
        Load_Order_Status()
        Load_Department()
        Load_repair_place()
        Load_Computer()
        Load_RepairDetail()
        Me.DTPReceiveDate.DateTime = Now
        Me.DtpFinishDate.DateTime = Now
        Me.TxtDocNo.Text = Get_NewOrderNO()
        on_load = False

    End Sub

    Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Me.Dispose()
    End Sub

  

    Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        Try
            Strsql = "Insert into repair_order (repair_order_no,repair_computer,repair_budget_year,repair_order_date,repair_department,repiar_staff,repair_finish_date"
            Strsql = Strsql & ",repair_place,repair_status,repair_remark,repair_survey_note,repair_technician_note,imagepart,repair_device,repair_non_computer"
            Strsql = Strsql & ") values ('" & Me.TxtDocNo.Text & "'," & CboComputer.SelectedValue & ","
            Strsql = Strsql & "'" & CboBudgetYear.Text & "','" & Pn_Framework.MysqlDateTimeFormat(DTPReceiveDate.DateTime, False) & "'"
            Strsql = Strsql & "," & CboDepartments.SelectedValue & "," & Me.CboEmployee.SelectedValue & ",'" & Pn_Framework.MysqlDateTimeFormat(DtpFinishDate.DateTime, False) & "'"
            Strsql = Strsql & "," & CboRepairPlace.SelectedValue & "," & CboStatus.SelectedValue & ",'" & Memo_repair_note.EditValue & "'"
            Strsql = Strsql & ",'" & Memo_Survey_Note.EditValue & "','" & Memo_Technician_Note.EditValue & "','" & Me.TxtImagepart.Text & "','" & Me.TxtDeviceName.Text & "'"
            If Me.ChkNonComputer.Checked = True Then
                Strsql = Strsql & ",1"
            Else
                Strsql = Strsql & ",0"
            End If
            Strsql = Strsql & ")"


            If _mysql.MySQLExecute(Strsql) >= 0 Then
                MsgBox("บันทึกข้อมูลเอกสารเรียบร้อย", MsgBoxStyle.Information, "ผลการบันทึกข้อมูล")

                If Primary_Key = -1 Then
                    Primary_Key = Pn_Framework.Get_LastNumericInsertID()
                End If

                Me.DialogResult = Windows.Forms.DialogResult.OK
            Else

                With frmDebug
                    .lblFormName.Text = Me.Name
                    .lblFunctionName.Text = "BtnSave_Click"
                    .MemoErr_Description.Text = "กรุณาตรวจสอบคำสั่ง sql"
                    .MemoSQL.Text = Strsql
                    .ShowDialog()
                End With
            End If

            TxtOrderNO.NullText = Primary_Key
            For Each DR In DS.Tables(0).Rows
                DR("repair_order_id") = Primary_Key
                If DR("repair_qty").ToString = "" Then
                    DR("repair_qty") = 1
                End If
            Next
            DA.Update(DS.Tables(0))
            Me.Dispose()

        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Get_NewOrderNO"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub

    Private Sub PanelControl1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PanelControl1.Paint

    End Sub

    Private Sub CboDepartments_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboDepartments.SelectedIndexChanged
        If on_load = True Then Exit Sub
        Load_Computer()
    End Sub



    Private Sub ChkNonComputer_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChkNonComputer.CheckedChanged
        If Me.TxtDeviceName.Properties.ReadOnly = True Then
            Me.CboComputer.Enabled = False
            Me.TxtDeviceName.Properties.ReadOnly = False
        Else
            Me.CboComputer.Enabled = False
            Me.TxtDeviceName.Properties.ReadOnly = True
        End If
    End Sub
End Class